﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Diagnostics.Contracts;

namespace ReactiveCode.Util.Expressions
{
    class ExpressionReplacer: ExpressionVisitor
    {
        Expression _what, _with;

        public Expression Replace(Expression expression, Expression what, Expression with)
        {
            Contract.Requires(expression != null && what != null && with != null);
            _what = what;
            _with = with;
            return Visit(expression);
        }

        public override Expression Visit(Expression node)
        {
            if (node == _what)
                return _with;
            return base.Visit(node);
        }
    }
}
